package com.qhhc.controller;

import com.qhhc.entity.CarDealer;
import com.qhhc.service.ICarDealerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
public class CarDealerController {

    @Autowired
    private ICarDealerService carDealerService;

    /**
     * 获取所有经销商
     */
    @GetMapping("/api/dealers")
    public List<CarDealer> getAllDealers() {
        return carDealerService.getAllDealers();
    }

    /**
     * 分页获取经销商
     */
    @GetMapping("/api/dealers/page")
    public Map<String, Object> getDealersByPage(
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize) {
        return carDealerService.getDealersByPage(pageNum, pageSize);
    }

    /**
     * 根据ID获取经销商
     */
    @GetMapping("/api/dealers/{id}")
    public CarDealer getDealerById(@PathVariable Long id) {
        return carDealerService.getDealerById(id);
    }

    /**
     * 根据用户ID获取经销商
     */
    @GetMapping("/api/dealers/user/{userId}")
    public CarDealer getDealerByUserId(@PathVariable Long userId) {
        return carDealerService.getDealerByUserId(userId);
    }

    /**
     * 条件查询经销商
     */
    @PostMapping("/api/dealers/search")
    public List<CarDealer> searchDealers(@RequestBody Map<String, Object> params) {
        return carDealerService.searchDealers(params);
    }

    /**
     * 添加经销商
     */
    @PostMapping("/api/dealers")
    public Map<String, Object> addDealer(@RequestBody CarDealer dealer) {
        Map<String, Object> result = new HashMap<>();
        int rows = carDealerService.addDealer(dealer);
        if (rows > 0) {
            result.put("success", true);
            result.put("message", "添加成功");
            result.put("data", dealer);
        } else {
            result.put("success", false);
            result.put("message", "添加失败");
        }
        return result;
    }

    /**
     * 更新经销商
     */
    @PutMapping("/api/dealers")
    public Map<String, Object> updateDealer(@RequestBody CarDealer dealer) {
        Map<String, Object> result = new HashMap<>();
        int rows = carDealerService.updateDealer(dealer);
        if (rows > 0) {
            result.put("success", true);
            result.put("message", "更新成功");
        } else {
            result.put("success", false);
            result.put("message", "更新失败");
        }
        return result;
    }

    /**
     * 删除经销商
     */
    @DeleteMapping("/api/dealers/{id}")
    public Map<String, Object> deleteDealer(@PathVariable Long id) {
        Map<String, Object> result = new HashMap<>();
        int rows = carDealerService.deleteDealer(id);
        if (rows > 0) {
            result.put("success", true);
            result.put("message", "删除成功");
        } else {
            result.put("success", false);
            result.put("message", "删除失败");
        }
        return result;
    }

    /**
     * 根据用户ID删除经销商
     */
    @DeleteMapping("/api/dealers/user/{userId}")
    public Map<String, Object> deleteDealerByUserId(@PathVariable Long userId) {
        Map<String, Object> result = new HashMap<>();
        int rows = carDealerService.deleteDealerByUserId(userId);
        if (rows > 0) {
            result.put("success", true);
            result.put("message", "删除成功");
        } else {
            result.put("success", false);
            result.put("message", "删除失败");
        }
        return result;
    }
} 